package cn.elead.chaos.remote.web.remote.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import cn.elead.chaos.core.constant.UserConstants;
import cn.elead.chaos.core.enums.BusinessType;
import cn.elead.chaos.excel.ExcelUtil;
import cn.elead.chaos.framework.monitor.operlog.Log;
import cn.elead.chaos.framework.web.global.ApiAssert;
import cn.elead.chaos.framework.web.global.ErrorCodeEnum;
import cn.elead.chaos.framework.web.global.response.UnifiedReturn;
import cn.elead.chaos.mybatils.web.controller.BaseController;
import cn.elead.chaos.mybatils.web.page.TableDataInfo;
import cn.elead.chaos.sso.core.util.SsoUtils;
import cn.elead.chaos.remote.web.remote.domain.SysDictType;
import cn.elead.chaos.remote.web.remote.service.ISysDictTypeService;

/**
 * 数据字典信息
 * 
 * @author luopeng
 */
@UnifiedReturn
@RestController
@RequestMapping("/system/dict/type")
public class RemoteDictTypeController extends BaseController {

	@Autowired
	private ISysDictTypeService dictTypeService;

	@GetMapping("/page")
	public TableDataInfo<SysDictType> page(SysDictType dictType) {
		startPage();
		List<SysDictType> list = dictTypeService.selectDictTypeList(dictType);
		return getDataTable(list);
	}

	@GetMapping("/export")
	@Log(title = "字典类型", businessType = BusinessType.EXPORT)
	public String export(SysDictType dictType) {
		List<SysDictType> list = dictTypeService.selectDictTypeList(dictType);
		ExcelUtil<SysDictType> util = new ExcelUtil<SysDictType>(SysDictType.class);
		return util.exportExcel(list, "字典类型");
	}

	/**
	 * 查询字典类型详细
	 */
	@GetMapping(value = "/{dictId}")
	public SysDictType getInfo(@PathVariable Long dictId) {
		return dictTypeService.selectDictTypeById(dictId);
	}

	/**
	 * 新增字典类型
	 */
	@PostMapping
	@Log(title = "字典类型", businessType = BusinessType.INSERT)
	public int add(@Validated @RequestBody SysDictType dict) {
		if (UserConstants.NOT_UNIQUE.equals(dictTypeService.checkDictTypeUnique(dict))) {
			ApiAssert.failure(ErrorCodeEnum.BAD_REQUEST.convert("新增字典'" + dict.getDictName() + "'失败，字典类型已存在"));
		}
		dict.setCreateBy(SsoUtils.getUserName());
		return dictTypeService.insertDictType(dict);
	}

	/**
	 * 修改字典类型
	 */
	@PutMapping
	@Log(title = "字典类型", businessType = BusinessType.UPDATE)
	public int edit(@Validated @RequestBody SysDictType dict) {
		if (UserConstants.NOT_UNIQUE.equals(dictTypeService.checkDictTypeUnique(dict))) {
			ApiAssert.failure(ErrorCodeEnum.BAD_REQUEST.convert("修改字典'" + dict.getDictName() + "'失败，字典类型已存在"));
		}
		dict.setUpdateBy(SsoUtils.getUserName());
		return dictTypeService.updateDictType(dict);
	}

	/**
	 * 删除字典类型
	 */
	@DeleteMapping("/{dictIds}")
	@Log(title = "字典类型", businessType = BusinessType.DELETE)
	public int remove(@PathVariable Long[] dictIds) {
		return dictTypeService.deleteDictTypeByIds(dictIds);
	}

	/**
	 * 获取字典选择框列表
	 */
	@GetMapping("/optionselect")
	public List<SysDictType> optionselect() {
		List<SysDictType> dictTypes = dictTypeService.selectDictTypeAll();
		return dictTypes;
	}
}
